package com.googlecode.kipler.syntax.formula;

import com.googlecode.kipler.syntax.Role;

/**
 * 
 * @author İnanç Seylan
 * 
 */
public class ObjectRoleAssertion extends Formula {

	private Role role;

	private String owner;

	private String filler;

	public ObjectRoleAssertion() {

	}

	public ObjectRoleAssertion(Role role, String owner, String filler) {
		setRole(role);
		setOwner(owner);
		setFiller(filler);
	}

	public String getFiller() {
		return filler.toString();
	}

	public String getOwner() {
		return owner.toString();
	}

	public Role getRole() {
		return role;
	}

	public void setFiller(String filler) {
		this.filler = filler;
	}

	public void setOwner(String owner) {
		this.owner = owner;
	}

	public void setRole(Role role) {
		this.role = role;
	}

	/**
	 * @see tr.edu.ege.seagent.alcbi.syntax.Formula#equals(java.lang.Object)
	 */
	@Override
	public boolean equals(Object obj) {
		if (obj != null && (obj.getClass().equals(this.getClass()))) {
			ObjectRoleAssertion other = (ObjectRoleAssertion) obj;
			return (this.getRole().equals(other.getRole())
					&& owner.equals(other.owner) && filler.equals(other.filler))
					|| (this.getRole().copy().toggleInverse().equals(
							other.getRole())
							&& owner.equals(other.filler) && filler
							.equals(other.owner));
		}
		return false;
	}

	/**
	 * @see tr.edu.ege.seagent.alcbi.syntax.Formula#hashCode()
	 */
	@Override
	public int hashCode() {
		return 17 * 37 + getRole().hashCode() + getOwner().hashCode()
				+ getFiller().hashCode();
	}

	public ObjectRoleAssertion copy() {
		ObjectRoleAssertion ra = new ObjectRoleAssertion();
		ra.role = role.copy();
		ra.owner = owner;
		ra.filler = filler;
		return ra;
	}

	/**
	 * @see java.lang.Object#toString()
	 */
	@Override
	public String toString() {
		StringBuffer buffer = new StringBuffer();
		buffer.append(getRole().toString());
		buffer.append("(");
		buffer.append(getOwner().toString());
		buffer.append(", ");
		buffer
				.append(getFiller().toString());
		buffer.append(")");
		if (isNegated()) {
			buffer.insert(0, "not(");
			buffer.append(")");
		}
		return buffer.toString();
	}

	public void accept(FormulaVisitor v) {
		v.visitObjectRoleAssertion(this);
	}

	public ObjectRoleAssertion toggleNegated() {
		return this;
	}

	public boolean isNegated() {
		return false;
	}

	public ObjectRoleAssertion normalize(String expectedOwner) {
		ObjectRoleAssertion result = this.copy();
		if (getFiller().equals(expectedOwner)) {
			result.setOwner(getFiller());
			result.setFiller(getOwner());
			result.setRole(getRole().copy().toggleInverse());
		}
		return result;
	}

}
